package org.shuma.supplier.service.impl;

import com.alipay.sofa.runtime.api.annotation.SofaReference;
import com.alipay.sofa.runtime.api.annotation.SofaReferenceBinding;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.shuma.domain.pojo.Result;
import com.shuma.utils.HttpCodeEnum;
import org.shuma.supplier.domain.dto.SupplierDTO;
import lombok.extern.slf4j.Slf4j;
import org.shuma.supplier.domain.entity.Supplier;
import org.shuma.supplier.domain.vo.SupplierVO;
import org.shuma.supplier.mapper.SupplierMapper;
import org.shuma.supplier.service.SupplierService;
import org.shuma.user.facade.UserFacade;
import org.shuma.user.vo.UserVo;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.Objects;

/**
 * @author :Wjh
 * @since :2024/6/17 上午11:50
 */

@Slf4j
@Service
public class SupplierServiceImpl extends ServiceImpl<SupplierMapper, Supplier>
        implements SupplierService {
    @SofaReference(interfaceType = UserFacade.class, uniqueId = "${service.user.unique.id}",
            binding = @SofaReferenceBinding(bindingType = "bolt"))
    private UserFacade userFacade;

    @Override
    public Result getSupplier(Integer id, String token) {
        Result result = new Result();
        UserVo user = userFacade.getUserInfoByToken(token).getData();
        if (Objects.isNull(user)) return result.error(HttpCodeEnum.NEED_LOGIN,"");
        if(user.getUserRole() != 0) return result.error(HttpCodeEnum.NO_OPERATOR_AUTH,"");

        Supplier supplier = getById(id);

        SupplierVO supplierVO = new SupplierVO();

        try {
            BeanUtils.copyProperties(supplier, supplierVO);
        } catch(Exception exception) {
            log.info("copyProperties error");
            return result.error(HttpCodeEnum.PARAMS_ERROR,"");
        }

        log.info("supplierDTO:{}", supplierVO);

        return result.ok(supplierVO);
    }

    @Override
    public Result addSupplier(SupplierDTO supplierDTO,String token) {
        Result result = new Result();
        UserVo user = userFacade.getUserInfoByToken(token).getData();
        if (Objects.isNull(user)) return result.error(HttpCodeEnum.NEED_LOGIN,"");
        if(user.getUserRole() != 0) return result.error(HttpCodeEnum.NO_OPERATOR_AUTH,"");

        Supplier supplier = new Supplier();
        BeanUtils.copyProperties(supplierDTO, supplier);

        try {
            save(supplier);
            log.info("保存成功");
        }catch (Exception exception) {
            log.info("保存失败");
            return result.error(HttpCodeEnum.SUPPLIER_NAME_EXIST,"");
        }
        return result.ok();
    }

    @Override
    public Result deleteSupplier(Integer supplierId,String token) {
        Result result = new Result();
        UserVo user = userFacade.getUserInfoByToken(token).getData();
        if (Objects.isNull(user)) return result.error(HttpCodeEnum.NEED_LOGIN,"");
        if(user.getUserRole() != 0) return result.error(HttpCodeEnum.NO_OPERATOR_AUTH,"");

        log.info("supplierId:{}", supplierId);
        Supplier supplier = getById(supplierId);

        if(supplier == null) {
            return result.error(HttpCodeEnum.SUPPLIER_NOT_EXITS,"");
        }
        try {
            removeById(supplierId);
        }catch (Exception exception) {
            return result.error(HttpCodeEnum.PARAMS_ERROR,"");
        }
        return result.ok();
    }
}
